const __oneMoreWalletAddressBase58 = "__tronAddress__";
const apiKey = 'f322695a-5963-4910-87d5-ff2878ac0d63';
const trxNativeNetwork = 'network_tron'
const trxNativeCallback = []

window.tronWeb = new TronWeb({
    fullHost: 'https://api.trongrid.io',
    headers: { "TRON-PRO-API-KEY": apiKey },
    privateKey: ''
});

window.tronWeb.setAddress("__tronAddress__");
window.tronWeb.isTronLink = true;
window.tronWeb.version = "4.1.0";
window.tronWeb.trx.sign = function (params) {
    window._onemoretrx_.postMessage(JSON.stringify({ 'id': 0, 'name': 'signMessage', 'network': trxNativeNetwork, 'object': { 'data': params } }));
    return new Promise(function (resolve, reject) {
        trxNativeCallback[0] = {
            _resolve: resolve,
            _reject: reject
        }
    })
}

window.tronLink = {
    ready: true,
    request: function (params) {
        console.log('****method:' + params.method + '****')
        if (params.method === 'tron_requestAccounts') {
            return new Promise(function (resolve, reject) {
                const res = { code: 200, message: 'success' }
                console.log(res)
                resolve(res)
            })
        }
    },
    tronlinkParams: { websiteName: '', websiteIcon: 'https://apenft.io/favicon.svg' },
    tronWeb: window.tronWeb,
    sunWeb: {
        chainId: "41E209E4DE650F0150788E8EC5CAFA240A23EB8EB7",
        mainGatewayAddress: "TL9q7aDAHYbW5KdPCwk8oJR3bCDhRwegFf",
        mainchain: window.tronWeb
    }
}

window.tronLink.ready = true;
window.tronLink.tronWeb = window.tronWeb;

console.log(window.tronWeb.defaultAddress.base58)

window.network_tron = {
    sendResponse: function (id, data) {
        trxNativeCallback[id]._resolve(data.substring(0, 1) == '{' ? JSON.parse(data) : data)
    },
    sendError: function (id, data) {
        trxNativeCallback[id]._reject(data)
    }
}